cmake_minimum_required(VERSION 3.12)
project(Lite)

set(BUILD_LITE "on")

include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/secure_option.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/compile_link_option.cmake)

#Options that can be configured through environment variables or manually
set(MSLITE_GPU_BACKEND "" CACHE STRING "enable gpu backend, \
    opencl only support arm64 and x86_64 , tensorrt only support x86_64, opencl/cuda/tensorrt/off")
set(MSLITE_REGISTRY_DEVICE "off" CACHE STRING "Compile Mindspore Lite that supports specific devices, \
    currently supported devices: Hi3516D/Hi3519A/Hi3559A/SD3403")
set(MSLITE_MINDDATA_IMPLEMENT "lite_cv" CACHE STRING "off, lite_cv, cloud, or full")
option(MSLITE_ENABLE_NPU "enable npu, only arm64 or arm32 support" off)
option(MSLITE_ENABLE_TRAIN "enable train" on)
option(MSLITE_ENABLE_SSE "enable SSE instruction set, only x86_64 support" off)
option(MSLITE_ENABLE_AVX "enable AVX instruction set, only x86_64 support" off)
option(MSLITE_ENABLE_AVX512 "enable AVX512 instruction set, only x86_64 support" off)
option(MSLITE_ENABLE_CONVERTER "enable converter, only x86_64 support" on)
option(MSLITE_ENABLE_TOOLS "enable tools" on)
option(MSLITE_ENABLE_TESTCASES "enable testcase" off)
option(MSLITE_ENABLE_RUNTIME_PASS "enable runtime pass" on)
option(MSLITE_ENABLE_HIGH_PERFORMANCE "enable high performance" off)
option(MSLITE_ENABLE_STRING_KERNEL "enable string kernel" on)
option(MSLITE_ENABLE_CONTROLFLOW "enable control and tensorlist" on)
option(MSLITE_ENABLE_AUTO_PARALLEL "enable automatic parallelism" on)
option(MSLITE_ENABLE_WEIGHT_DECODE "enable weight decode" on)
option(MSLITE_ENABLE_CUSTOM_KERNEL "enable extend kernel registry" on)
option(MSLITE_ENABLE_MINDRT "enable mindrt use" on)
option(MSLITE_ENABLE_DELEGATE "enable delegate use" on)
option(MSLITE_ENABLE_V0 "support v0 schema" on)
option(MSLITE_ENABLE_FP16 "Whether to compile Fp16 operator" off)
option(MSLITE_ENABLE_INT8 "Whether to compile Int8 operator" on)
option(MSLITE_ENABLE_ACL "enable ACL" off)
option(MSLITE_ENABLE_MODEL_ENCRYPTION "enable model encryption, only converter support" on)
option(MSLITE_ENABLE_SPARSE_COMPUTE "enable sparse kernel" off)
option(MSLITE_ENABLE_RUNTIME_CONVERT "enable runtime convert" off)
option(MSLITE_ENABLE_RUNTIME_GLOG "enable runtime glog" off)
option(MSLITE_ENABLE_COVERAGE "enable code coverage" off)
option(MSLITE_ENABLE_SHARING_MEM_WITH_OPENGL "enable sharing memory with OpenGL" off)
option(MSLITE_ENABLE_SERVER_INFERENCE "enable inference on server" off)

#Option that can be configured through manually
option(ENABLE_VERBOSE "" off)
option(ENABLE_MODEL_OBF "if support model obfuscation" off)
if(MACHINE_LINUX_ARM64)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8.2-a+fp16")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8.2-a+fp16")
endif()

if(DEFINED ENV{MSLITE_GPU_BACKEND})
    set(MSLITE_GPU_BACKEND $ENV{MSLITE_GPU_BACKEND})
endif()
if(DEFINED ENV{MSLITE_REGISTRY_DEVICE})
    set(MSLITE_REGISTRY_DEVICE $ENV{MSLITE_REGISTRY_DEVICE})
endif()
if(DEFINED ENV{MSLITE_ENABLE_NPU})
    set(MSLITE_ENABLE_NPU $ENV{MSLITE_ENABLE_NPU})
endif()
if(DEFINED ENV{MSLITE_ENABLE_TRAIN})
    set(MSLITE_ENABLE_TRAIN $ENV{MSLITE_ENABLE_TRAIN})
endif()
if(DEFINED ENV{MSLITE_ENABLE_SSE})
    set(MSLITE_ENABLE_SSE $ENV{MSLITE_ENABLE_SSE})
endif()
if(DEFINED ENV{MSLITE_ENABLE_AVX})
    set(MSLITE_ENABLE_AVX $ENV{MSLITE_ENABLE_AVX})
endif()
if(DEFINED ENV{MSLITE_ENABLE_AVX512})
    set(MSLITE_ENABLE_AVX512 $ENV{MSLITE_ENABLE_AVX512})
endif()
if(DEFINED ENV{MSLITE_ENABLE_CONVERTER})
    set(MSLITE_ENABLE_CONVERTER $ENV{MSLITE_ENABLE_CONVERTER})
endif()
if(DEFINED ENV{MSLITE_ENABLE_TOOLS})
    set(MSLITE_ENABLE_TOOLS $ENV{MSLITE_ENABLE_TOOLS})
endif()
if(DEFINED ENV{MSLITE_ENABLE_TESTCASES})
    set(MSLITE_ENABLE_TESTCASES $ENV{MSLITE_ENABLE_TESTCASES})
endif()
if(DEFINED ENV{MSLITE_ENABLE_RUNTIME_PASS})
    set(MSLITE_ENABLE_RUNTIME_PASS $ENV{MSLITE_ENABLE_RUNTIME_PASS})
endif()
if(DEFINED ENV{MSLITE_ENABLE_HIGH_PERFORMANCE})
    set(MSLITE_ENABLE_HIGH_PERFORMANCE $ENV{MSLITE_ENABLE_HIGH_PERFORMANCE})
endif()
if(DEFINED ENV{MSLITE_ENABLE_STRING_KERNEL})
    set(MSLITE_ENABLE_STRING_KERNEL $ENV{MSLITE_ENABLE_STRING_KERNEL})
endif()
if(DEFINED ENV{MSLITE_ENABLE_CONTROLFLOW})
    set(MSLITE_ENABLE_CONTROLFLOW $ENV{MSLITE_ENABLE_CONTROLFLOW})
endif()
if(DEFINED ENV{MSLITE_ENABLE_AUTO_PARALLEL})
    set(MSLITE_ENABLE_AUTO_PARALLEL $ENV{MSLITE_ENABLE_AUTO_PARALLEL})
endif()
if(DEFINED ENV{MSLITE_ENABLE_WEIGHT_DECODE})
    set(MSLITE_ENABLE_WEIGHT_DECODE $ENV{MSLITE_ENABLE_WEIGHT_DECODE})
endif()
if(DEFINED ENV{MSLITE_ENABLE_CUSTOM_KERNEL})
    set(MSLITE_ENABLE_CUSTOM_KERNEL $ENV{MSLITE_ENABLE_CUSTOM_KERNEL})
endif()
if(DEFINED ENV{MSLITE_ENABLE_MINDRT})
    set(MSLITE_ENABLE_MINDRT $ENV{MSLITE_ENABLE_MINDRT})
endif()
if(DEFINED ENV{MSLITE_ENABLE_DELEGATE})
    set(MSLITE_ENABLE_DELEGATE $ENV{MSLITE_ENABLE_DELEGATE})
endif()
if(DEFINED ENV{MSLITE_ENABLE_V0})
    set(MSLITE_ENABLE_V0 $ENV{MSLITE_ENABLE_V0})
endif()
if(DEFINED ENV{MSLITE_ENABLE_FP16})
    set(MSLITE_ENABLE_FP16 $ENV{MSLITE_ENABLE_FP16})
endif()
if(DEFINED ENV{MSLITE_ENABLE_INT8})
    set(MSLITE_ENABLE_INT8 $ENV{MSLITE_ENABLE_INT8})
endif()
if(DEFINED ENV{MSLITE_ENABLE_SPARSE_COMPUTE})
    set(MSLITE_ENABLE_SPARSE_COMPUTE $ENV{MSLITE_ENABLE_SPARSE_COMPUTE})
endif()
if(DEFINED ENV{MSLITE_ENABLE_ACL})
    set(MSLITE_ENABLE_ACL $ENV{MSLITE_ENABLE_ACL})
endif()
if(DEFINED ENV{MSLITE_MINDDATA_IMPLEMENT})
    set(MSLITE_MINDDATA_IMPLEMENT $ENV{MSLITE_MINDDATA_IMPLEMENT})
endif()
if(DEFINED ENV{MSLITE_ENABLE_MODEL_ENCRYPTION})
    set(MSLITE_ENABLE_MODEL_ENCRYPTION $ENV{MSLITE_ENABLE_MODEL_ENCRYPTION})
endif()

if(DEFINED ENV{MSLITE_ENABLE_RUNTIME_CONVERT})
    set(MSLITE_ENABLE_RUNTIME_CONVERT $ENV{MSLITE_ENABLE_RUNTIME_CONVERT})
endif()
if(DEFINED ENV{MSLITE_ENABLE_RUNTIME_GLOG})
    set(MSLITE_ENABLE_RUNTIME_GLOG $ENV{MSLITE_ENABLE_RUNTIME_GLOG})
endif()

if(DEFINED ENV{MSLITE_ENABLE_COVERAGE})
    set(MSLITE_ENABLE_COVERAGE $ENV{MSLITE_ENABLE_COVERAGE})
endif()
if(DEFINED ENV{MSLITE_ENABLE_SHARING_MEM_WITH_OPENGL})
    set(MSLITE_ENABLE_SHARING_MEM_WITH_OPENGL $ENV{MSLITE_ENABLE_SHARING_MEM_WITH_OPENGL})
endif()
if(DEFINED ENV{MSLITE_ENABLE_SERVING})
    set(MSLITE_ENABLE_SERVING $ENV{MSLITE_ENABLE_SERVING})
endif()
if(DEFINED ENV{MSLITE_ENABLE_SERVER_INFERENCE})
    set(MSLITE_ENABLE_SERVER_INFERENCE $ENV{MSLITE_ENABLE_SERVER_INFERENCE})
endif()

if(MACHINE_LINUX_ARM64)
    add_compile_definitions(MACHINE_LINUX_ARM64)
endif()
if(TOOLCHAIN_NAME STREQUAL "himix200")
    set(TARGET_HIMIX on)
    set(TARGET_HIMIX200 on)
    add_compile_definitions(SUPPORT_NNIE)
elseif(TOOLCHAIN_NAME STREQUAL "himix100")
    set(TARGET_HIMIX on)
    set(TARGET_HIMIX100 on)
    add_compile_definitions(SUPPORT_NNIE)
elseif(TOOLCHAIN_NAME STREQUAL "mix210")
    set(TARGET_MIX210 on)
    add_compile_definitions(SUPPORT_34XX)
elseif(TOOLCHAIN_NAME STREQUAL "ohos-lite")
    set(TARGET_OHOS_LITE on)
    SET_PROPERTY(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
endif()
if(MSLITE_ENABLE_SERVER_INFERENCE)
    add_compile_definitions(SERVER_INFERENCE)
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.3.0
        AND NOT TARGET_HIMIX AND NOT TARGET_MIX210)
    message(FATAL_ERROR "GCC version ${CMAKE_CXX_COMPILER_VERSION} must not be less than 7.3.0")
endif()

if(NOT MSLITE_ENABLE_ACL)
    set(ENABLE_GLIBCXX ON)
else()
    set(MSLITE_ENABLE_TRAIN off)
endif()

if(PLATFORM_ARM64)
    if(MSLITE_GPU_BACKEND STREQUAL "")
        set(MSLITE_GPU_BACKEND "opencl")
    endif()
    if((NOT MSLITE_GPU_BACKEND STREQUAL "opencl") AND (NOT MSLITE_GPU_BACKEND STREQUAL "off"))
        message("invalid MSLITE_GPU_BACKEND value ${MSLITE_GPU_BACKEND} for arm64, MSLITE_GPU_BACKEND is set to off.")
        set(MSLITE_GPU_BACKEND "off")
    endif()
elseif(PLATFORM_ARM32)
    if((NOT MSLITE_GPU_BACKEND STREQUAL "opencl") AND (NOT MSLITE_GPU_BACKEND STREQUAL "off") AND
    (NOT MSLITE_GPU_BACKEND STREQUAL ""))
        message("invalid MSLITE_GPU_BACKEND value ${MSLITE_GPU_BACKEND} for arm32, MSLITE_GPU_BACKEND is set to off.")
        set(MSLITE_GPU_BACKEND "off")
    endif()
elseif(WIN32)
    set(MSLITE_GPU_BACKEND "off")
else()
    if(${MSLITE_REGISTRY_DEVICE}  STREQUAL "SD3403")
        set(MSLITE_ENABLE_DPICO_ATC_ADAPTER on)
    endif()
    if(MSLITE_GPU_BACKEND STREQUAL "")
        set(MSLITE_GPU_BACKEND "off")
    endif()
    if((NOT MSLITE_GPU_BACKEND STREQUAL "tensorrt") AND (NOT MSLITE_GPU_BACKEND STREQUAL "off") AND
    (NOT MSLITE_GPU_BACKEND STREQUAL "opencl"))
        message("invalid MSLITE_GPU_BACKEND value ${MSLITE_GPU_BACKEND} for x86_64, MSLITE_GPU_BACKEND is set to off.")
        set(MSLITE_GPU_BACKEND "off")
    endif()
endif()

if(PLATFORM_ARM64 OR PLATFORM_ARM32)
    set(PLATFORM_ARM "on")
    set(MSLITE_ENABLE_SSE off)
    set(MSLITE_ENABLE_AVX off)
    set(MSLITE_ENABLE_AVX512 off)
    if(NOT MACHINE_LINUX_ARM64)
        set(MSLITE_ENABLE_CONVERTER off)
    endif()
    set(MSLITE_ENABLE_RUNTIME_GLOG off)
    set(MSLITE_ENABLE_RUNTIME_CONVERT off)
#set for cross - compiling toolchain
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
else()
    set(MSLITE_ENABLE_NPU off)
endif()

if(MSLITE_ENABLE_SSE OR MSLITE_ENABLE_AVX OR MSLITE_ENABLE_AVX512 OR WIN32)
    set(MSLITE_ENABLE_RUNTIME_CONVERT off)
endif()

if(MSLITE_ENABLE_TRAIN AND NOT MSLITE_ENABLE_WEIGHT_DECODE)
    message(FATAL_ERROR "If MSLITE_ENABLE_WEIGHT_DECODE use if configured as off, "
            "MSLITE_ENABLE_TRAIN must also be configured as off")
endif()

if(MSLITE_ENABLE_CONTROLFLOW AND NOT MSLITE_ENABLE_MINDRT)
    message(FATAL_ERROR "If MSLITE_ENABLE_MINDRT use if configured as off, "
            "MSLITE_ENABLE_CONTROLFLOW must also be configured as off")
endif()

if(MSLITE_ENABLE_RUNTIME_CONVERT)
    set(MSLITE_ENABLE_RUNTIME_GLOG on)
    set(MSLITE_ENABLE_CONVERTER on)
endif()

if(MSLITE_ENABLE_TRAIN)
    set(SUPPORT_TRAIN on)
    if(NOT MSLITE_MINDDATA_IMPLEMENT STREQUAL "off" OR NOT PLATFORM_ARM)
        set(MSLITE_MINDDATA_IMPLEMENT full)
    endif()
endif()

if(MSLITE_ENABLE_NPU)
    set(SUPPORT_NPU on)
    if(NOT PLATFORM_ARM)
        message(FATAL_ERROR "NPU only support platform arm.")
    endif()
    if(DEFINED ENV{HWHIAI_DDK})
        message("HWHIAI_DDK=$ENV{HWHIAI_DDK}")
    else()
        message(FATAL_ERROR "please set HWHIAI_DDK, example: export HWHIAI_DDK=/root/usr/hwhiai-ddk-100.510.010.010/")
    endif()
endif()

if(TARGET_HIMIX OR TARGET_OHOS_LITE)
  set(MSLITE_ENABLE_MINDRT off)
endif()

if(MSVC)
  set(MSLITE_ENABLE_CONVERTER off)
endif()

message(STATUS "************MindSpore Lite Build Option:************")
message(STATUS "\tMSLITE_GPU_BACKEND                = \t${MSLITE_GPU_BACKEND}")
message(STATUS "\tMSLITE_REGISTRY_DEVICE            = \t${MSLITE_REGISTRY_DEVICE}")
message(STATUS "\tMSLITE_ENABLE_NPU                 = \t${MSLITE_ENABLE_NPU}")
message(STATUS "\tMSLITE_ENABLE_TRAIN               = \t${MSLITE_ENABLE_TRAIN}")
message(STATUS "\tMSLITE_ENABLE_SSE                 = \t${MSLITE_ENABLE_SSE}")
message(STATUS "\tMSLITE_ENABLE_AVX                 = \t${MSLITE_ENABLE_AVX}")
message(STATUS "\tMSLITE_ENABLE_AVX512              = \t${MSLITE_ENABLE_AVX512}")
message(STATUS "\tMSLITE_ENABLE_CONVERTER           = \t${MSLITE_ENABLE_CONVERTER}")
message(STATUS "\tMSLITE_ENABLE_TOOLS               = \t${MSLITE_ENABLE_TOOLS}")
message(STATUS "\tMSLITE_ENABLE_TESTCASES           = \t${MSLITE_ENABLE_TESTCASES}")
message(STATUS "\tMSLITE_ENABLE_HIGH_PERFORMANCE    = \t${MSLITE_ENABLE_HIGH_PERFORMANCE}")
message(STATUS "\tMSLITE_ENABLE_RUNTIME_PASS        = \t${MSLITE_ENABLE_RUNTIME_PASS}")
message(STATUS "\tMSLITE_ENABLE_STRING_KERNEL       = \t${MSLITE_ENABLE_STRING_KERNEL}")
message(STATUS "\tMSLITE_ENABLE_CONTROLFLOW         = \t${MSLITE_ENABLE_CONTROLFLOW}")
message(STATUS "\tMSLITE_ENABLE_AUTO_PARALLEL       = \t${MSLITE_ENABLE_AUTO_PARALLEL}")
message(STATUS "\tMSLITE_ENABLE_WEIGHT_DECODE       = \t${MSLITE_ENABLE_WEIGHT_DECODE}")
message(STATUS "\tMSLITE_ENABLE_CUSTOM_KERNEL       = \t${MSLITE_ENABLE_CUSTOM_KERNEL}")
message(STATUS "\tMSLITE_ENABLE_MINDRT              = \t${MSLITE_ENABLE_MINDRT}")
message(STATUS "\tMSLITE_ENABLE_V0                  = \t${MSLITE_ENABLE_V0}")
message(STATUS "\tMSLITE_MINDDATA_IMPLEMENT         = \t${MSLITE_MINDDATA_IMPLEMENT}")
message(STATUS "\tMSLITE_ENABLE_DELEGATE            = \t${MSLITE_ENABLE_DELEGATE}")
message(STATUS "\tMSLITE_ENABLE_ACL                 = \t${MSLITE_ENABLE_ACL}")
message(STATUS "\tMSLITE_ENABLE_FP16                = \t${MSLITE_ENABLE_FP16}")
message(STATUS "\tMSLITE_ENABLE_INT8                = \t${MSLITE_ENABLE_INT8}")
message(STATUS "\tMSLITE_ENABLE_MODEL_ENCRYPTION    = \t${MSLITE_ENABLE_MODEL_ENCRYPTION}")
message(STATUS "\tMSLITE_ENABLE_SPARSE_COMPUTE      = \t${MSLITE_ENABLE_SPARSE_COMPUTE}")
message(STATUS "\tMSLITE_ENABLE_RUNTIME_CONVERT     = \t${MSLITE_ENABLE_RUNTIME_CONVERT}")
message(STATUS "\tMSLITE_ENABLE_RUNTIME_GLOG        = \t${MSLITE_ENABLE_RUNTIME_GLOG}")
message(STATUS "\tMSLITE_ENABLE_COVERAGE            = \t${MSLITE_ENABLE_COVERAGE}")
message(STATUS "\tMSLITE_ENABLE_SHARING_MEM_WITH_OPENGL      = \t${MSLITE_ENABLE_SHARING_MEM_WITH_OPENGL}")
message(STATUS "\tMSLITE_ENABLE_SERVER_INFERENCE      = \t${MSLITE_ENABLE_SERVER_INFERENCE}")

if((MSLITE_ENABLE_CONVERTER OR MSLITE_ENABLE_TESTCASES) AND (
        NOT MSLITE_ENABLE_MINDRT
        OR NOT MSLITE_ENABLE_STRING_KERNEL
        OR NOT MSLITE_ENABLE_CONTROLFLOW
        OR NOT MSLITE_ENABLE_WEIGHT_DECODE
        OR NOT MSLITE_ENABLE_CUSTOM_KERNEL))
    message(FATAL_ERROR "If one of 'MSLITE_ENABLE_MINDRT MSLITE_ENABLE_STRING_KERNEL "
            "MSLITE_ENABLE_CONTROLFLOW MSLITE_ENABLE_WEIGHT_DECODE MSLITE_ENABLE_CUSTOM_KERNEL'"
            "is configured as off, MSLITE_ENABLE_CONVERTER and MSLITE_ENABLE_TESTCASES must also be configured as off")
endif()

if(((MSLITE_GPU_BACKEND STREQUAL tensorrt) OR MSLITE_ENABLE_NPU) AND (
        NOT MSLITE_ENABLE_DELEGATE))
    message(FATAL_ERROR "If MSLITE_ENABLE_DELEGATE use is configured as off, MSLITE_ENABLE_NPU must also be configured
    as off and MSLITE_GPU_BACKEND nor can it be configured as tensorrt.")
endif()

if(MSLITE_ENABLE_FP16 AND PLATFORM_ARM32 AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
        AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
    message(STATUS "If you want to build fp16 in arm82_a32, \
    your Clang version:[${CMAKE_CXX_COMPILER_VERSION}] must not be less than 9.0 and please use android nkd r21e!")
    set(MSLITE_ENABLE_FP16 off)
endif()

if(MSLITE_ENABLE_HIGH_PERFORMANCE)
    add_compile_definitions(ENABLE_HIGH_PERFORMANCE)
endif()

if(ENABLE_ASAN)
    add_definitions(-fsanitize=address -fno-omit-frame-pointer)
    if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
        add_definitions(-mllvm -asan-use-private-alias=1)
    endif()
    add_link_options(-fsanitize=address)
endif()

if(MSLITE_ENABLE_COVERAGE)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g --coverage -fprofile-arcs -ftest-coverage")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g --coverage -fprofile-arcs -ftest-coverage")
endif()

if(MSLITE_ENABLE_ACL)
    add_definitions(-D ENABLE_LITE_ACL)
    add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--allow-shlib-undefined")

    if(DEFINED ENV{ASCEND_CUSTOM_PATH})
        set(ASCEND_PATH $ENV{ASCEND_CUSTOM_PATH})
    else()
        set(ASCEND_PATH /usr/local/Ascend)
    endif()
    set(ASCEND_RUNTIME_PATH ${ASCEND_PATH}/fwkacllib/lib64)
    set(ASCEND_TOOLKIT_RUNTIME_PATH ${ASCEND_PATH}/ascend-toolkit/latest/fwkacllib/lib64)
endif()

set(PKG_NAME_PREFIX mindspore-lite-${MS_VERSION_MAJOR}.${MS_VERSION_MINOR}.${MS_VERSION_REVISION})

if(SUPPORT_NPU)
    set(DDK_PATH "$ENV{HWHIAI_DDK}/ddk/ai_ddk_lib")
    set(DDK_INCLUDE_PATH "$ENV{HWHIAI_DDK}/ddk/ai_ddk_lib/include")
    if(PLATFORM_ARM64)
        set(DDK_LIB_PATH ${DDK_PATH}/lib64)
    elseif(PLATFORM_ARM32)
        set(DDK_LIB_PATH ${DDK_PATH}/lib)
    endif()
    add_compile_definitions(SUPPORT_NPU)
endif()

add_compile_definitions(NO_DLIB)

if(NOT MSVC)
    add_compile_options(-fPIC)
endif()

if(PLATFORM_ARM64)
    if(TARGET_HIMIX100 OR TARGET_MIX210 OR MACHINE_LINUX_ARM64)
        set(RUNTIME_COMPONENT_NAME "linux-aarch64")
    else()
        set(RUNTIME_COMPONENT_NAME "android-aarch64")
    endif()
elseif(PLATFORM_ARM32)
    set(RUNTIME_COMPONENT_NAME "android-aarch32")
    if(TARGET_HIMIX200)
        set(RUNTIME_COMPONENT_NAME "linux-aarch32")
    elseif(TARGET_OHOS_LITE)
        set(RUNTIME_COMPONENT_NAME "ohos-aarch32")
    endif()
elseif(WIN32)
    if(CMAKE_SIZEOF_VOID_P EQUAL 4)
        set(RUNTIME_COMPONENT_NAME "win-x86")
    else()
        set(RUNTIME_COMPONENT_NAME "win-x64")
    endif()
else()
    set(RUNTIME_COMPONENT_NAME "linux-x64")
endif()

string(REPLACE "/mindspore/lite" "" TOP_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(CORE_DIR ${TOP_DIR}/mindspore/core)
set(CCSRC_DIR ${TOP_DIR}/mindspore/ccsrc)
set(NNACL_DIR ${CCSRC_DIR}/backend/kernel_compiler/cpu/nnacl)
include_directories(${TOP_DIR})
include_directories(${CORE_DIR})
include_directories(${CORE_DIR}/ir)
include_directories(${CCSRC_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/kernel/arm)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/)
include_directories(${TOP_DIR}/third_party)
include_directories(${CMAKE_BINARY_DIR})
include_directories(${CCSRC_DIR}/minddata/dataset)

include(${TOP_DIR}/cmake/utils.cmake)
include(${TOP_DIR}/cmake/dependency_utils.cmake)
include(${TOP_DIR}/cmake/dependency_securec.cmake)
include(${TOP_DIR}/cmake/external_libs/flatbuffers.cmake)

if(MSLITE_GPU_BACKEND STREQUAL opencl)
    include(${TOP_DIR}/cmake/external_libs/opencl.cmake)
endif()

if(MSLITE_ENABLE_CONVERTER OR MSLITE_MINDDATA_IMPLEMENT STREQUAL "full" OR MSLITE_MINDDATA_IMPLEMENT STREQUAL "wrapper"
    OR MSLITE_ENABLE_TOOLS)
    include(${TOP_DIR}/cmake/external_libs/json.cmake)
endif()

if(DEFINED ARCHS)
    add_definitions(-DMS_COMPILE_IOS)
endif()

file(GLOB FBS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/schema/*.fbs)
ms_build_flatbuffers_lite(FBS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/schema/ fbs_src ${CMAKE_BINARY_DIR}/schema "")
ms_build_flatbuffers_lite(FBS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/schema/ fbs_inner_src ${CMAKE_BINARY_DIR}/schema/inner
        "inner")

if(ENABLE_VERBOSE)
    set(CMAKE_VERBOSE_MAKEFILE on)
endif()
if(SUPPORT_TRAIN)
    add_compile_definitions(SUPPORT_TRAIN)
endif()
if(ENABLE_NEON)
    add_compile_definitions(ENABLE_NEON)
endif()
if(MSLITE_ENABLE_FP16)
    add_compile_definitions(ENABLE_FP16)
    if(PLATFORM_ARM32)
        add_compile_definitions(ENABLE_ARM82_A32)
    endif()
endif()
if(MSLITE_GPU_BACKEND STREQUAL opencl)
    add_definitions(-DGPU_OPENCL)
    gene_opencl(${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/kernel/opencl/cl)
    gene_opencl(${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/kernel/opencl/cl/int8)
    add_definitions(-DUSE_OPENCL_WRAPPER)
    add_definitions(-DMS_OPENCL_PROFILE=false)
    add_definitions(-DCL_TARGET_OPENCL_VERSION=200)
    add_definitions(-DCL_HPP_TARGET_OPENCL_VERSION=120)
    add_definitions(-DCL_HPP_MINIMUM_OPENCL_VERSION=120)
    add_compile_definitions(SUPPORT_GPU)
    include_directories(${CMAKE_BINARY_DIR}/_deps/opencl-headers-src/)
    include_directories(${CMAKE_BINARY_DIR}/_deps/opencl-clhpp-src/include)
    if(MSLITE_ENABLE_SHARING_MEM_WITH_OPENGL)
        add_definitions(-DENABLE_OPENGL_TEXTURE)
    endif()
endif()

if(MSLITE_GPU_BACKEND STREQUAL cuda)
    add_definitions(-DGPU_CUDA)
    add_compile_definitions(SUPPORT_GPU)
endif()
if(MSLITE_GPU_BACKEND STREQUAL tensorrt)
    add_compile_definitions(SUPPORT_GPU)
    set(SUPPORT_TENSORRT on)
    if(DEFINED ENV{TENSORRT_PATH})
        message("TENSORRT_PATH = $ENV{TENSORRT_PATH}")
    else()
        message(FATAL_ERROR "please set TENSORRT_PATH, example: export TENSORRT_PATH=/root/usr/TensorRT-6.0.1.5/")
    endif()
    if(DEFINED ENV{CUDA_HOME})
        message("CUDA_HOME = $ENV{CUDA_HOME}")
    else()
        message(FATAL_ERROR "please set CUDA_HOME, example: export CUDA_HOME=/usr/local/cuda-10.1/")
    endif()
endif()

if(WIN32)
    add_compile_definitions(BUILDING_DLL)
endif()

include_directories(${CORE_DIR}/mindrt/include)
include_directories(${CORE_DIR}/mindrt/src)

if(NOT WIN32 AND NOT APPLE)
    if(ENABLE_MODEL_OBF)
        add_compile_definitions(ENABLE_MODEL_OBF)
    endif()
endif()

if(ENABLE_MODEL_OBF)
    if(PLATFORM_ARM32)
        set(OBF_LIB_DIR ${TOP_DIR}/mindspore/lite/tools/obfuscator/lib/android-aarch32)
    elseif(PLATFORM_ARM64)
        set(OBF_LIB_DIR ${TOP_DIR}/mindspore/lite/tools/obfuscator/lib/android-aarch64)
    else()
        set(OBF_LIB_DIR ${TOP_DIR}/mindspore/lite/tools/obfuscator/lib/linux-x64)
    endif()
    set(OBF_LIBS libmsdeobfuscator-lite.so)
endif()

if(MSLITE_ENABLE_CONVERTER OR MSLITE_ENABLE_RUNTIME_GLOG)
    include(${TOP_DIR}/cmake/external_libs/glog.cmake)
endif()

if(MSLITE_ENABLE_CONVERTER)
    include(${TOP_DIR}/cmake/external_libs/opencv.cmake)
    include_directories(${PYTHON_INCLUDE_DIRS})
    include(${TOP_DIR}/cmake/external_libs/eigen.cmake)
    include(${TOP_DIR}/cmake/external_libs/protobuf.cmake)
    if(MSLITE_ENABLE_MODEL_ENCRYPTION)
        find_package(Patch)
        include(${TOP_DIR}/cmake/external_libs/openssl.cmake)
    endif()
    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/converter)
endif()

if(MSLITE_ENABLE_MINDRT)
    add_compile_definitions(ENABLE_MINDRT)
endif()

if(PLATFORM_ARM32)
    add_definitions(-mfloat-abi=softfp -mfpu=neon)
    add_compile_definitions(ENABLE_ARM32)
    add_compile_definitions(ENABLE_ARM)
endif()
if(PLATFORM_ARM64)
    add_compile_definitions(ENABLE_ARM64)
    add_compile_definitions(ENABLE_ARM)
endif()

if(NOT PLATFORM_ARM)
    if(MSLITE_ENABLE_AVX512)
        set(X86_64_SIMD "avx512")
        add_compile_definitions(ENABLE_SSE)
        add_compile_definitions(ENABLE_AVX)
        add_compile_definitions(ENABLE_AVX512)
        if(NOT MSVC)
            set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx -mfma -mavx512f")
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx -mfma -mavx512f")
        endif()
    elseif(MSLITE_ENABLE_AVX)
        set(X86_64_SIMD "avx")
        add_compile_definitions(ENABLE_SSE)
        add_compile_definitions(ENABLE_AVX)
        if(NOT MSVC)
            set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx -mfma")
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx -mfma")
        endif()
    elseif(MSLITE_ENABLE_SSE)
        set(X86_64_SIMD "sse")
        add_compile_definitions(ENABLE_SSE)
        if(NOT MSVC)
            set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.1")
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.1")
        endif()
    endif()
endif()

if(MSLITE_MINDDATA_IMPLEMENT STREQUAL "lite" OR MSLITE_MINDDATA_IMPLEMENT STREQUAL "full"
    OR MSLITE_MINDDATA_IMPLEMENT STREQUAL "wrapper")
    add_compile_definitions(ENABLE_ANDROID)
    if(NOT PLATFORM_ARM32 AND NOT PLATFORM_ARM64)
        add_compile_definitions(ENABLE_MD_LITE_X86_64)
    endif()
    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/minddata)
endif()

if(MSLITE_MINDDATA_IMPLEMENT STREQUAL "lite_cv")
    add_compile_definitions(ENABLE_ANDROID)
    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/minddata)
endif()

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/ops)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/micro/coder)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src)
add_subdirectory(${CCSRC_DIR}/backend/kernel_compiler/cpu/nnacl build)

if(MSLITE_ENABLE_TOOLS)
    if(NOT MSLITE_COMPILE_TWICE)
        add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/benchmark)
    endif()
    if(SUPPORT_TRAIN)
        add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/benchmark_train)
    endif()
    if(NOT PLATFORM_ARM AND NOT WIN32)
        add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/cropper)
        add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/schema_gen)
        add_dependencies(fbs_src gen_ops)
        add_dependencies(fbs_inner_src gen_ops)
    endif()
endif()

if(NOT WIN32 AND MSLITE_ENABLE_TESTCASES AND NOT MSLITE_MINDDATA_IMPLEMENT STREQUAL "off")
    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/test)
endif()

if(NOT APPLE)
    include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/file_list.cmake)
    include(${TOP_DIR}/cmake/package_lite.cmake)
endif()
